package com.example.jeong.yuejian10.Fragment;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.jeong.yuejian10.Activity.SearchClubActivity;
import com.example.jeong.yuejian10.Adapter.ClubListAdapter;
import com.example.jeong.yuejian10.Bean.ClubBean;
import com.example.jeong.yuejian10.Listener.RetrofitListener;
import com.example.jeong.yuejian10.Model.ClubModel;
import com.example.jeong.yuejian10.R;

import java.util.List;

//俱乐部页
public class ClubFragment extends Fragment implements View.OnClickListener,RetrofitListener<List<ClubBean>> {
    private RecyclerView recyclerView;
    private View view = null;
    private ImageView c_pic;
    private TextView c_name;
    private ClubListAdapter adapter;
    private ImageView search_club;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.tab02, container, false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_club);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(6);
        recyclerView.addItemDecoration(decoration);
        initView();
        getClublist();
        initEvents();
        return view;
    }
    public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public SpacesItemDecoration(int space) {
            this.space=space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
            outRect.right=space;
            outRect.bottom=space;
            if(parent.getChildAdapterPosition(view)==0){
                outRect.top=space;
            }
        }
    }
    private void initEvents(){
        //搜索
        search_club.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(ClubFragment.this.getActivity(), SearchClubActivity.class);
                startActivity(intent);

            }
        });

    }
    private void initView()
    {
        c_name= (TextView) view.findViewById(R.id.c_name);
        c_pic= (ImageView) view.findViewById(R.id.c_pic);

        search_club = (ImageView) view.findViewById(R.id.search_club);
    }

    void getClublist(){
        ClubModel clubModel=new ClubModel();
        clubModel.getClublist(this);
    }

    @Override
    public void onSuccess(List<ClubBean> clubBeen){
        adapter = new ClubListAdapter(ClubFragment.this.getActivity(),clubBeen);
        recyclerView.setAdapter(adapter);
    }

    @Override
    public void onFail() {
        //showToast("网络发生错误");
    }
    @Override
    public void onClick(View view) {

    }
}
